<?php

/**
 * @author: 大向 <http://star.x2e.cn>
 * @Email: xiangmingxing@55haitao.com
 * @Date:   2020-12-14 18:03:46
 * @Last Modified by:   大向
 */

namespace StarXiang\OpenSearch;

use Illuminate\Support\ServiceProvider;
use OpenSearch\Client\OpenSearchClient;
use OpenSearch\Client\SearchClient;

class OpenSearchServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $config = realpath($path = __DIR__."/../config/aliyun.php") ?: $path;

        $this->mergeConfigFrom($config, "aliyun");
    }

    public function register()
    {
        $this->app->singleton("SearchClient", function ($app) {
            $config = $app->make("config")->get("aliyun");
            $openSearchClient = new OpenSearchClient(
                $config["opensearch"]["access_key"],
                $config["opensearch"]["access_secret"],
                $config["opensearch"]["host"],
                $config["opensearch"]["debug"]
            );

            return new SearchClient($openSearchClient);
        });
    }
}
